from sqlmodel import SQLModel, Field, create_engine, Session
from typing import Optional
from config import *

# 定义数据库URL和创建数据库引擎
DATABASE_URL = f"mysql+pymysql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_DATABASE}"
engine = create_engine(DATABASE_URL)


# 创建数据库和表
def create_engine_and_tables():
    SQLModel.metadata.create_all(engine)


# 依赖项，用于获取数据库会话
def get_db():
    db = Session(engine)
    try:
        yield db
    finally:
        db.close()
